package com.cd2cd.code_template.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cd2cd.code_template.code_template.domain.SysAuth;
import com.cd2cd.code_template.code_template.domain.SysUser;
import com.cd2cd.code_template.code_template.domain.gen.SysUserCriteria;
import com.cd2cd.code_template.code_template.mapper.SysAuthMapper;
import com.cd2cd.code_template.code_template.mapper.SysUserMapper;
import com.cd2cd.code_template.configuration.constants.DBConstants;
import com.cd2cd.code_template.configuration.constants.ServiceCode;
import com.cd2cd.code_template.configuration.security.Md5PasswordEncoder;
import com.cd2cd.code_template.configuration.security.TokenUser;
import com.cd2cd.code_template.configuration.security.jwt.JwtUser;
import com.cd2cd.code_template.configuration.security.loginUserService.LoadUserService;
import com.cd2cd.code_template.configuration.security.loginUserService.LoadUserService.Backend;
import com.cd2cd.code_template.service.LoginService;
import com.cd2cd.code_template.vo.BaseRes;
import com.cd2cd.code_template.vo.LoginResVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.SneakyThrows;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.util.Assert;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private JwtUser jwtUser;

    @Resource
    private Backend backend;

    @Resource
    private LoadUserService.SysAdmin sysAdmin;

    @Value("${system.login.account:}")
    private String sysAccount;

    @Value("${system.login.password:}")
    private String sysPassword;


    @Resource
    private SysUserMapper sysUserMapper;

    public static void main(String[] args) {
        System.out.println(Md5PasswordEncoder.md5Encode("111111"));
    }

    @SneakyThrows
    @Override
    public BaseRes<LoginResVo> adminLogin(String username, String password) {

        password = Md5PasswordEncoder.md5Encode(password);

        SysUserCriteria suCriteria = new SysUserCriteria();
        suCriteria.createCriteria()
                .andUsernameEqualTo(username)
                .andPasswordEqualTo(password)
                .andDelFlagEqualTo("ok")
                .andStatusEqualTo(DBConstants.Status.ENABLE);
        List<SysUser> users = sysUserMapper.selectByExample(suCriteria);

        if (CollectionUtils.isEmpty(users)) {
            return new BaseRes<>(ServiceCode.LOGIN_ERROR);
        }

        BaseRes<LoginResVo> res = new BaseRes<>(ServiceCode.SUCCESS);


        TokenUser tokenUser = backend.loadUserByUsername(username);
        String token = jwtUser.getToken(tokenUser);

        Set<String> authSet = tokenUser.getAuthIds();

        LoginResVo loginResVo = new LoginResVo();
        loginResVo.setAuthIds(authSet);
        loginResVo.setType(LoadUserService.Backend.class.getSimpleName());
        loginResVo.setToken(token);
        res.setData(loginResVo);

        return res;
    }

    @Override
    @SneakyThrows
    public BaseRes<LoginResVo> systemLogin(SysUser sysUser) {
        BaseRes<LoginResVo> res = new BaseRes<>(ServiceCode.LOGIN_ERROR);

        String username = sysUser.getUsername();
        String pas = sysUser.getPassword();

        assert StringUtils.isNotBlank(sysAccount) : "系统登录账号尚未配置！";

        if (!(sysAccount.equals(username) && sysPassword.equals(pas))) {
            return res;
        }


        TokenUser tokenUser = sysAdmin.loadUserByUsername(username);

        Set<String> authSet = tokenUser.getAuthIds();

        String token = jwtUser.getToken(tokenUser);

        LoginResVo loginResVo = new LoginResVo();
        loginResVo.setAuthIds(authSet);
        loginResVo.setType(LoadUserService.SysAdmin.class.getSimpleName());
        loginResVo.setToken(token);
        res.setData(loginResVo);
        res.setServiceCode(ServiceCode.SUCCESS);

        return res;
    }
}
